path_proj = here::here()
path_source = file.path(path_proj, "source")

source(file.path(path_source, "simulation", "simulations_functions_final.R"))
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
## 
##     smiths
source(file.path(path_source, "functions", "plot_function.R"))
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
source(file.path(path_source, "functions", "fit_function.R"))
source(file.path(path_source, "functions", "table_function.R"))

# place for draws
# mac
posterior_draws_path = file.path(Sys.getenv("HOME"), "Desktop", "draws", "testEach")
# windows
#posterior_draws_path = file.path(Sys.getenv("USERPROFILE"), "Desktop", "draws", "testEach")

#data path
data_save_path = file.path(path_proj, "data", "fitted_model", "simulation", "1. q_constant")
#models
q_constant <- file.path(path_proj, "source", "models", 
                      "q-constant.stan")
b_constant <- file.path(path_proj, "source", "models", 
                     "b-constant.stan")
b_rw <- file.path(path_proj, "source", "models", 
                     "b-rw1.stan")
b_ou <-  file.path(path_proj, "source", "models", 
                      "b-ou.stan")

compiled_models <- list(
  q_constant = cmdstan_model(q_constant),
  b_constant = cmdstan_model(b_constant),
  b_rw = cmdstan_model(b_rw),
  b_ou = cmdstan_model(b_ou)
)

models_to_use <- c("q_constant", "b_constant", "b_rw", "b_ou")

simulate data - FR

setting

###### setting #####
seed <- 123
set.seed(seed)

# data
alpha_increase_seq_1 <- seq(10, 750, by = 30)
alpha_decrease_seq_1 <- seq(750, 10, by = -30)
alpha_lamb =  c( rep(10,5), alpha_increase_seq_1 + rnorm(alpha_increase_seq_1,10,10), 
                 alpha_decrease_seq_1 + rnorm(alpha_decrease_seq_1,10,10),
                 rep(10,5))
beta_lamb = 0.5
T = 60
# reprot delay
D <- 15;

# Time period for checking
D_check <- 5

first_date <- as.Date("2024-01-01")

scoreRange <- c(first_date+days(9), first_date+days(19), first_date+days(29),
                first_date+days(39), first_date+days(49))
# q_constant_FR
params_q_constant_FR <- list(
  data = list(
    alpha_lamb = alpha_lamb,  
    beta_lamb  = beta_lamb,
    T       = T,
    date_start = first_date,
    D = D
  ),
  q_model = list(
    method        = "q_constant",
    method_params = list(b = 0.7, phi = 0.9)
  )
)

q_constant_FR <- simulateData(params_q_constant_FR)
# q_constant_NFR
params_q_constant_NFR <- list(
  data = list(
    alpha_lamb = alpha_lamb,  
    beta_lamb  = beta_lamb,
    T       = T,
    date_start = first_date,
    D = D
  ),
  q_model = list(
    method        = "q_constant",
    method_params = list(b = 0.1, phi = 0.9)
  )
)

q_constant_NFR <- simulateData(params_q_constant_NFR)

expplot

# exploritary analysis
page_num <- ceiling(nrow(q_constant_FR$case_reported_cumulated)/16)
exp_plot_q_constant <- fit_exp_plot(q_constant_FR$case_reported_cumulated,ncol = 4, nrow = 4, page = c(1:page_num), if_fit = T)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(exp_plot_q_constant)
## $plots
## $plots[[1]]

## 
## $plots[[2]]

## 
## $plots[[3]]

## 
## $plots[[4]]

## 
## 
## $coefficients
##            b         phi
## 1  0.5055333 0.068284699
## 2  0.5547189 0.061080516
## 3  0.7367248 0.068106350
## 4  1.0819349 0.002559996
## 5  0.8801655 0.052014065
## 6  0.7714955 0.073444643
## 7  0.6618841 0.082137331
## 8  0.7350497 0.101364168
## 9  0.6569562 0.067900661
## 10 0.7529487 0.119118549
## 11 0.6962667 0.100999200
## 12 0.6908501 0.087611444
## 13 0.7260712 0.105252887
## 14 0.7246881 0.069474558
## 15 0.7242846 0.102580180
## 16 0.6505762 0.113664245
## 17 0.6765901 0.084812645
## 18 0.7023962 0.076889900
## 19 0.6865660 0.108069599
## 20 0.7423705 0.095698268
## 21 0.7499525 0.096562958
## 22 0.7147604 0.105208998
## 23 0.7015282 0.089256697
## 24 0.7142811 0.096966733
## 25 0.7040346 0.084828227
## 26 0.6710329 0.085590224
## 27 0.7251725 0.094808391
## 28 0.6969755 0.094929707
## 29 0.6935165 0.101173557
## 30 0.7213171 0.108125228
## 31 0.7450026 0.094426217
## 32 0.6959939 0.105698896
## 33 0.6771576 0.099930732
## 34 0.7228220 0.110606835
## 35 0.7062824 0.095197996
## 36 0.7482875 0.081456830
## 37 0.7035790 0.116396157
## 38 0.6718279 0.130207795
## 39 0.7056283 0.092995274
## 40 0.7051379 0.100951520
## 41 0.6758787 0.100670058
## 42 0.7037410 0.088612736
## 43 0.7023240 0.072963928
## 44 0.6665807 0.081973264
## 45 0.6860273 0.100502178
## 46 0.7179368 0.088329028
## 47 0.7208294 0.103174027
## 48 0.6311429 0.101095981
## 49 0.7561507 0.113951339
## 50 0.6426656 0.092764470
## 51 0.6830333 0.101369111
## 52 0.6920994 0.079863195
## 53 0.6688082 0.081973827
## 54 0.6752151 0.117209703
## 55 0.7067910 0.063220636
## 56 0.8115918 0.221940971
## 57 0.7492494 0.134753278
## 58 0.6864893 0.041201336
## 59 0.6850925 0.061430331
## 60 0.5849492 0.038381252
exp_plot_q_constant$coefficients
##            b         phi
## 1  0.5055333 0.068284699
## 2  0.5547189 0.061080516
## 3  0.7367248 0.068106350
## 4  1.0819349 0.002559996
## 5  0.8801655 0.052014065
## 6  0.7714955 0.073444643
## 7  0.6618841 0.082137331
## 8  0.7350497 0.101364168
## 9  0.6569562 0.067900661
## 10 0.7529487 0.119118549
## 11 0.6962667 0.100999200
## 12 0.6908501 0.087611444
## 13 0.7260712 0.105252887
## 14 0.7246881 0.069474558
## 15 0.7242846 0.102580180
## 16 0.6505762 0.113664245
## 17 0.6765901 0.084812645
## 18 0.7023962 0.076889900
## 19 0.6865660 0.108069599
## 20 0.7423705 0.095698268
## 21 0.7499525 0.096562958
## 22 0.7147604 0.105208998
## 23 0.7015282 0.089256697
## 24 0.7142811 0.096966733
## 25 0.7040346 0.084828227
## 26 0.6710329 0.085590224
## 27 0.7251725 0.094808391
## 28 0.6969755 0.094929707
## 29 0.6935165 0.101173557
## 30 0.7213171 0.108125228
## 31 0.7450026 0.094426217
## 32 0.6959939 0.105698896
## 33 0.6771576 0.099930732
## 34 0.7228220 0.110606835
## 35 0.7062824 0.095197996
## 36 0.7482875 0.081456830
## 37 0.7035790 0.116396157
## 38 0.6718279 0.130207795
## 39 0.7056283 0.092995274
## 40 0.7051379 0.100951520
## 41 0.6758787 0.100670058
## 42 0.7037410 0.088612736
## 43 0.7023240 0.072963928
## 44 0.6665807 0.081973264
## 45 0.6860273 0.100502178
## 46 0.7179368 0.088329028
## 47 0.7208294 0.103174027
## 48 0.6311429 0.101095981
## 49 0.7561507 0.113951339
## 50 0.6426656 0.092764470
## 51 0.6830333 0.101369111
## 52 0.6920994 0.079863195
## 53 0.6688082 0.081973827
## 54 0.6752151 0.117209703
## 55 0.7067910 0.063220636
## 56 0.8115918 0.221940971
## 57 0.7492494 0.134753278
## 58 0.6864893 0.041201336
## 59 0.6850925 0.061430331
## 60 0.5849492 0.038381252
exp_b_data_q_constant<- data.frame( date = as.Date(rownames(q_constant_FR$case_reported_cumulated)),
                          b = exp_plot_q_constant$coefficients$b)

exp_b_plot_q_constant <- ggplot(exp_b_data_q_constant, aes(x = date, y = b)) +
  geom_point(color = "black", size = 1.5) +       
  geom_smooth(method = "loess", se = TRUE,        
              color = "blue", fill = "grey", alpha = 0.5) +
  theme_minimal() +
  labs(x = NULL, y = "Y", title = "Smoothed Curve of parameter b")

print(exp_b_plot_q_constant)
## `geom_smooth()` using formula = 'y ~ x'

# exploritary analysis
page_num <- ceiling(nrow(q_constant_NFR$case_reported_cumulated)/16)
exp_plot_q_constant <- fit_exp_plot(q_constant_NFR$case_reported_cumulated,ncol = 4, nrow = 4, page = c(1:page_num), if_fit = T)
print(exp_plot_q_constant)
## $plots
## $plots[[1]]

## 
## $plots[[2]]

## 
## $plots[[3]]

## 
## $plots[[4]]

## 
## 
## $coefficients
##            b          phi
## 1  0.1869946 -0.051001292
## 2  0.2164268  0.182978516
## 3  0.2322592  0.006665343
## 4  0.1226886  0.058841139
## 5  0.1339608  0.095413495
## 6  0.1448781  0.073409970
## 7  0.1869024  0.081341838
## 8  0.1740489  0.085342272
## 9  0.1679732  0.105031397
## 10 0.1562647  0.059657544
## 11 0.1602249  0.112346045
## 12 0.1594944  0.051112774
## 13 0.1604827  0.080301253
## 14 0.1750573  0.081823130
## 15 0.1659627  0.062328155
## 16 0.1561349  0.123258797
## 17 0.1615412  0.080535533
## 18 0.1586075  0.094762094
## 19 0.1665503  0.091766855
## 20 0.1668306  0.079700003
## 21 0.1640436  0.060326591
## 22 0.1681521  0.070888959
## 23 0.1688694  0.070063237
## 24 0.1590684  0.076071431
## 25 0.1707159  0.066487319
## 26 0.1677933  0.078593650
## 27 0.1662205  0.097874077
## 28 0.1710149  0.070876171
## 29 0.1607356  0.082807334
## 30 0.1708570  0.094916517
## 31 0.1688358  0.083794619
## 32 0.1724922  0.084044119
## 33 0.1592873  0.064842692
## 34 0.1706777  0.076979756
## 35 0.1632874  0.087194990
## 36 0.1689219  0.071887629
## 37 0.1751177  0.084623374
## 38 0.1683295  0.079353454
## 39 0.1571803  0.084286223
## 40 0.1665454  0.061380520
## 41 0.1638938  0.084909013
## 42 0.1731742  0.078762578
## 43 0.1738430  0.062129233
## 44 0.1619723  0.090494047
## 45 0.1637851  0.093924222
## 46 0.1624054  0.077472768
## 47 0.1628068  0.064622423
## 48 0.1778563  0.125616206
## 49 0.1771074  0.064658509
## 50 0.1558917  0.067076838
## 51 0.1705677  0.077697590
## 52 0.1950076  0.057534643
## 53 0.1581472  0.044804716
## 54 0.2011045  0.019402256
## 55 0.1778789  0.066105321
## 56 0.1722104  0.004072393
## 57 0.1856203  0.017189050
## 58 0.1653274  0.127217678
## 59 0.1801947 -0.074344767
## 60 0.1878984  0.298906638
exp_plot_q_constant$coefficients
##            b          phi
## 1  0.1869946 -0.051001292
## 2  0.2164268  0.182978516
## 3  0.2322592  0.006665343
## 4  0.1226886  0.058841139
## 5  0.1339608  0.095413495
## 6  0.1448781  0.073409970
## 7  0.1869024  0.081341838
## 8  0.1740489  0.085342272
## 9  0.1679732  0.105031397
## 10 0.1562647  0.059657544
## 11 0.1602249  0.112346045
## 12 0.1594944  0.051112774
## 13 0.1604827  0.080301253
## 14 0.1750573  0.081823130
## 15 0.1659627  0.062328155
## 16 0.1561349  0.123258797
## 17 0.1615412  0.080535533
## 18 0.1586075  0.094762094
## 19 0.1665503  0.091766855
## 20 0.1668306  0.079700003
## 21 0.1640436  0.060326591
## 22 0.1681521  0.070888959
## 23 0.1688694  0.070063237
## 24 0.1590684  0.076071431
## 25 0.1707159  0.066487319
## 26 0.1677933  0.078593650
## 27 0.1662205  0.097874077
## 28 0.1710149  0.070876171
## 29 0.1607356  0.082807334
## 30 0.1708570  0.094916517
## 31 0.1688358  0.083794619
## 32 0.1724922  0.084044119
## 33 0.1592873  0.064842692
## 34 0.1706777  0.076979756
## 35 0.1632874  0.087194990
## 36 0.1689219  0.071887629
## 37 0.1751177  0.084623374
## 38 0.1683295  0.079353454
## 39 0.1571803  0.084286223
## 40 0.1665454  0.061380520
## 41 0.1638938  0.084909013
## 42 0.1731742  0.078762578
## 43 0.1738430  0.062129233
## 44 0.1619723  0.090494047
## 45 0.1637851  0.093924222
## 46 0.1624054  0.077472768
## 47 0.1628068  0.064622423
## 48 0.1778563  0.125616206
## 49 0.1771074  0.064658509
## 50 0.1558917  0.067076838
## 51 0.1705677  0.077697590
## 52 0.1950076  0.057534643
## 53 0.1581472  0.044804716
## 54 0.2011045  0.019402256
## 55 0.1778789  0.066105321
## 56 0.1722104  0.004072393
## 57 0.1856203  0.017189050
## 58 0.1653274  0.127217678
## 59 0.1801947 -0.074344767
## 60 0.1878984  0.298906638
exp_b_data_q_constant<- data.frame( date = as.Date(rownames(q_constant_NFR$case_reported_cumulated)),
                          b = exp_plot_q_constant$coefficients$b)

exp_b_plot_q_constant <- ggplot(exp_b_data_q_constant, aes(x = date, y = b)) +
  geom_point(color = "black", size = 1.5) +       
  geom_smooth(method = "loess", se = TRUE,        
              color = "blue", fill = "grey", alpha = 0.5) +
  theme_minimal() +
  labs(x = NULL, y = "Y", title = "Smoothed Curve of parameter b")

print(exp_b_plot_q_constant)
## `geom_smooth()` using formula = 'y ~ x'

fit model

out_q_constant_FR <- nowcasting_moving_window(q_constant_FR$case_reported_cumulated, scoreRange =  scoreRange,
                          case_true = q_constant_FR$case_true,
                          start_date = first_date,
                          D = D, seeds = seed,
                          methods =models_to_use,
                          compiled_models = compiled_models,
                          iter_sampling = 2000, iter_warmup = 1000, refresh = 0,
                          num_chains = 3, suppress_output = T,
                          posterior_draws_path = file.path(posterior_draws_path, "q_constant")
                          )
## ====================
## now=2024-01-10 (1/5)
## ====================
## Warning in nowcasting_moving_window(q_constant_FR$case_reported_cumulated, :
## The number of rows of the input data is smaller than number of max delay D,
## which might cause inaccuracy.
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.8 seconds.
## Chain 2 finished in 0.6 seconds.
## Chain 3 finished in 0.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.7 seconds.
## Total execution time: 2.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.3 seconds.
## Chain 2 finished in 0.2 seconds.
## Chain 3 finished in 0.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.3 seconds.
## Total execution time: 1.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.9 seconds.
## Chain 2 finished in 3.0 seconds.
## Chain 3 finished in 1.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.2 seconds.
## Total execution time: 6.9 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.7 seconds.
## Chain 2 finished in 3.6 seconds.
## Chain 3 finished in 3.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.8 seconds.
## Total execution time: 8.8 seconds.
## 
## ====================
## now=2024-01-20 (2/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.2 seconds.
## Chain 2 finished in 1.3 seconds.
## Chain 3 finished in 1.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.2 seconds.
## Total execution time: 4.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.4 seconds.
## Chain 2 finished in 0.3 seconds.
## Chain 3 finished in 0.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.3 seconds.
## Total execution time: 1.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 3.8 seconds.
## Chain 2 finished in 5.5 seconds.
## Chain 3 finished in 6.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 5.2 seconds.
## Total execution time: 15.8 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 6.3 seconds.
## Chain 2 finished in 4.0 seconds.
## Chain 3 finished in 4.0 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 4.8 seconds.
## Total execution time: 14.6 seconds.
## 
## ====================
## now=2024-01-30 (3/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.9 seconds.
## Chain 2 finished in 1.9 seconds.
## Chain 3 finished in 1.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.9 seconds.
## Total execution time: 5.9 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.6 seconds.
## Chain 2 finished in 0.6 seconds.
## Chain 3 finished in 0.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.6 seconds.
## Total execution time: 2.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 8.6 seconds.
## Chain 2 finished in 8.9 seconds.
## Chain 3 finished in 8.6 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 8.7 seconds.
## Total execution time: 26.6 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 7.8 seconds.
## Chain 2 finished in 5.6 seconds.
## Chain 3 finished in 7.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 6.9 seconds.
## Total execution time: 20.9 seconds.
## 
## ====================
## now=2024-02-09 (4/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 3.0 seconds.
## Chain 2 finished in 3.0 seconds.
## Chain 3 finished in 2.9 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 3.0 seconds.
## Total execution time: 9.3 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.8 seconds.
## Chain 2 finished in 0.8 seconds.
## Chain 3 finished in 1.1 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.9 seconds.
## Total execution time: 3.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 10.7 seconds.
## Chain 2 finished in 10.0 seconds.
## Chain 3 finished in 12.9 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 11.2 seconds.
## Total execution time: 34.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 5.6 seconds.
## Chain 2 finished in 7.2 seconds.
## Chain 3 finished in 10.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 7.8 seconds.
## Total execution time: 23.7 seconds.
## 
## ====================
## now=2024-02-19 (5/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 3.4 seconds.
## Chain 2 finished in 3.7 seconds.
## Chain 3 finished in 3.4 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 3.5 seconds.
## Total execution time: 10.7 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.2 seconds.
## Chain 2 finished in 1.0 seconds.
## Chain 3 finished in 1.1 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.1 seconds.
## Total execution time: 3.5 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 20.3 seconds.
## Chain 2 finished in 18.1 seconds.
## Chain 3 finished in 15.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 18.1 seconds.
## Total execution time: 54.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 24.1 seconds.
## Chain 2 finished in 9.9 seconds.
## Chain 3 finished in 11.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 15.2 seconds.
## Total execution time: 45.9 seconds.
save(out_q_constant_FR, file = file.path(data_save_path, "FR_q_constant.RData"))
#load( file.path(data_save_path, "FR_q_constant.RData"))
# 
results_q_constant_FR <- nowcasts_table(out_q_constant_FR, D = D, report_unit = "day",
                          methods = models_to_use
                          )

plots_q_constant_FR <- nowcasts_plot(results_q_constant_FR, D = D, report_unit = "day",
                                  methods = models_to_use
                                  )
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
for (i in 1:length(results_q_constant_FR)) {
  print(calculate_metrics(results_q_constant_FR[[i]],
                          methods = models_to_use))
}
##     RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 157.46 85.67 92.62 81.61         181.01           0.5 q_constant
## 2  35.79 13.62 14.30  7.79          78.01           1.0 b_constant
## 3  30.38 11.78 12.66  6.89          94.11           1.0       b_rw
## 4  28.22 11.12 12.06  7.03          90.00           1.0       b_ou
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 54.72 11.32 41.10 11.05         114.11           0.9 q_constant
## 2  7.38  1.65  4.43  1.35          88.31           1.0 b_constant
## 3  7.70  1.50  4.66  1.29         101.36           1.0       b_rw
## 4  6.68  1.57  3.96  1.25          98.91           1.0       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 57.22  4.74 24.59 2.93         115.21          0.97 q_constant
## 2 44.77  3.59 11.45 1.46         109.64          1.00 b_constant
## 3 37.72  3.05 10.15 1.31         117.88          1.00       b_rw
## 4 43.37  3.46 10.83 1.35         116.24          1.00       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 18.10  1.85 13.03 1.56         116.76             1 q_constant
## 2  8.50  1.18  4.79 0.81         114.83             1 b_constant
## 3  9.83  1.24  4.58 0.76         117.83             1       b_rw
## 4  5.92  1.09  3.54 0.72         117.93             1       b_ou
##    RMSE RMSPE  MAE MAPE Interval_Width Coverage_Rate     Method
## 1 11.44  2.12 8.25 1.35         110.09             1 q_constant
## 2  9.32  2.15 4.43 0.98         109.27             1 b_constant
## 3 10.40  2.42 4.34 1.02         109.53             1       b_rw
## 4  9.32  2.21 4.12 0.98         109.91             1       b_ou
for (i in 1:length(results_q_constant_FR)) {
  print(calculate_metrics(data.table::last(results_q_constant_FR[[i]], D_check),
                          methods = models_to_use))
}
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 222.30 97.55 172.41 92.30         329.42             0 q_constant
## 2  50.61 18.74  27.80 11.30         138.21             1 b_constant
## 3  42.96 16.30  24.69 10.46         169.80             1       b_rw
## 4  39.89 15.07  23.27  9.77         161.40             1       b_ou
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 90.24 12.04 86.58 11.88         255.24             1 q_constant
## 2 14.42  2.11 13.79  2.00         196.63             1 b_constant
## 3 15.13  2.12 14.99  2.11         247.00             1       b_rw
## 4 13.10  1.94 12.54  1.83         237.21             1       b_ou
##     RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 134.31 10.18 82.13 6.17         274.62           0.8 q_constant
## 2 109.45  8.33 58.00 4.46         261.01           1.0 b_constant
## 3  92.16  7.02 50.37 3.88         303.04           1.0       b_rw
## 4 106.06  8.07 55.43 4.26         296.02           1.0       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 36.08  3.45 32.58 3.01         227.22             1 q_constant
## 2 21.74  2.10 20.14 1.88         223.43             1 b_constant
## 3 25.75  2.46 18.77 1.79         240.81             1       b_rw
## 4 13.58  1.19 11.76 1.03         244.41             1       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 26.07  5.81 22.57 4.85         146.62             1 q_constant
## 2 27.15  6.25 21.37 4.80         145.60             1 b_constant
## 3 31.71  7.24 25.72 5.63         153.22             1       b_rw
## 4 27.81  6.42 22.11 4.95         151.00             1       b_ou
plots_q_constant_FR
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

out_q_constant_NFR <- nowcasting_moving_window(q_constant_NFR$case_reported_cumulated, scoreRange = scoreRange,
                          case_true = q_constant_NFR$case_true,
                          start_date = first_date,
                          D = D, seeds = seed,
                          methods =models_to_use,
                          compiled_models = compiled_models,
                          iter_sampling = 2000, iter_warmup = 1000, refresh = 0,
                          num_chains = 3, suppress_output = T,
                          posterior_draws_path = file.path(posterior_draws_path, "q_constant")
                          )
## ====================
## now=2024-01-10 (1/5)
## ====================
## Warning in nowcasting_moving_window(q_constant_NFR$case_reported_cumulated, :
## The number of rows of the input data is smaller than number of max delay D,
## which might cause inaccuracy.
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.6 seconds.
## Chain 2 finished in 0.6 seconds.
## Chain 3 finished in 0.6 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.6 seconds.
## Total execution time: 2.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.3 seconds.
## Chain 2 finished in 0.3 seconds.
## Chain 3 finished in 0.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.3 seconds.
## Total execution time: 1.2 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.9 seconds.
## Chain 2 finished in 2.2 seconds.
## Chain 3 finished in 1.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.9 seconds.
## Total execution time: 6.2 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.1 seconds.
## Chain 2 finished in 4.4 seconds.
## Chain 3 finished in 3.8 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 3.4 seconds.
## Total execution time: 10.6 seconds.
## 
## ====================
## now=2024-01-20 (2/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.3 seconds.
## Chain 2 finished in 1.4 seconds.
## Chain 3 finished in 1.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.4 seconds.
## Total execution time: 4.3 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.9 seconds.
## Chain 2 finished in 0.9 seconds.
## Chain 3 finished in 1.0 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.9 seconds.
## Total execution time: 3.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 9.1 seconds.
## Chain 2 finished in 7.7 seconds.
## Chain 3 finished in 7.0 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 7.9 seconds.
## Total execution time: 24.2 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 5.7 seconds.
## Chain 2 finished in 5.8 seconds.
## Chain 3 finished in 5.4 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 5.6 seconds.
## Total execution time: 17.1 seconds.
## 
## ====================
## now=2024-01-30 (3/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.7 seconds.
## Chain 2 finished in 1.6 seconds.
## Chain 3 finished in 1.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.6 seconds.
## Total execution time: 5.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.5 seconds.
## Chain 2 finished in 1.2 seconds.
## Chain 3 finished in 1.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.3 seconds.
## Total execution time: 4.3 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 14.4 seconds.
## Chain 2 finished in 11.7 seconds.
## Chain 3 finished in 5.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 10.5 seconds.
## Total execution time: 31.7 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 6.7 seconds.
## Chain 2 finished in 9.8 seconds.
## Chain 3 finished in 7.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 7.9 seconds.
## Total execution time: 24.0 seconds.
## 
## ====================
## now=2024-02-09 (4/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.9 seconds.
## Chain 2 finished in 1.8 seconds.
## Chain 3 finished in 2.0 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.9 seconds.
## Total execution time: 6.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.2 seconds.
## Chain 2 finished in 2.2 seconds.
## Chain 3 finished in 1.8 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.1 seconds.
## Total execution time: 6.5 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 17.7 seconds.
## Chain 2 finished in 19.1 seconds.
## Chain 3 finished in 16.1 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 17.6 seconds.
## Total execution time: 53.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 9.9 seconds.
## Chain 2 finished in 12.2 seconds.
## Chain 3 finished in 11.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 11.3 seconds.
## Total execution time: 34.1 seconds.
## 
## ====================
## now=2024-02-19 (5/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.5 seconds.
## Chain 2 finished in 3.6 seconds.
## Chain 3 finished in 3.0 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 3.0 seconds.
## Total execution time: 9.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.2 seconds.
## Chain 2 finished in 2.4 seconds.
## Chain 3 finished in 2.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.4 seconds.
## Total execution time: 7.5 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 36.2 seconds.
## Chain 2 finished in 35.9 seconds.
## Chain 3 finished in 37.0 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 36.4 seconds.
## Total execution time: 109.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 17.0 seconds.
## Chain 2 finished in 14.3 seconds.
## Chain 3 finished in 15.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 15.7 seconds.
## Total execution time: 47.5 seconds.
save(out_q_constant_NFR, file = file.path(data_save_path, "NFR_q_constant.RData"))
#load( file.path(data_save_path, "NFR_q_constant.RData"))

results_q_constant_NFR <- nowcasts_table(out_q_constant_NFR, D = D, report_unit = "day", 
                          methods = models_to_use
                          )

plots_q_constant_NFR <- nowcasts_plot(results_q_constant_NFR, D = D, report_unit = "day", 
                                  methods = models_to_use
                                  )

for (i in 1:length(results_q_constant_NFR)) {
  print(calculate_metrics(results_q_constant_NFR[[i]],
                          methods = models_to_use))
}
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 36.58 20.70 17.67 16.52          89.91           1.0 q_constant
## 2 67.76 43.80 43.25 41.74          55.80           0.6 b_constant
## 3 75.73 47.06 48.13 44.76          58.31           0.6       b_rw
## 4 71.18 45.87 45.85 43.88          57.91           0.5       b_ou
##     RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 100.98 19.92 73.16 18.61         106.26          0.45 q_constant
## 2  60.76 11.51 38.69  9.97         126.71          0.95 b_constant
## 3  89.34 14.66 57.89 12.64         168.31          0.90       b_rw
## 4  68.05 12.32 43.71 10.66         151.31          0.95       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 165.92 20.03 131.74 19.36         109.51           0.2 q_constant
## 2  23.80  6.37  16.55  4.51         133.41           1.0 b_constant
## 3  53.24  6.60  34.33  5.45         196.38           1.0       b_rw
## 4  26.79  6.24  17.40  4.45         176.92           1.0       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 190.31 19.85 160.10 19.32         110.26          0.15 q_constant
## 2  23.59  5.70  16.21  3.62         134.86          1.00 b_constant
## 3  33.38  5.87  21.24  4.06         176.19          1.00       b_rw
## 4  22.18  5.69  15.33  3.56         159.94          1.00       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 182.01 19.98 154.70 19.42         102.63          0.16 q_constant
## 2  23.60  6.06  16.44  3.77         122.15          0.98 b_constant
## 3  23.01  5.88  16.13  3.78         139.49          1.00       b_rw
## 4  23.23  6.05  15.50  3.69         135.75          0.98       b_ou
for (i in 1:length(results_q_constant_NFR)) {
  print(calculate_metrics(data.table::last(results_q_constant_NFR[[i]],D_check),
                          methods = models_to_use))
}
##     RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1  51.63 20.08 32.43 14.85         155.61           1.0 q_constant
## 2  95.51 48.46 79.38 47.49          94.61           0.4 b_constant
## 3 106.81 53.61 88.98 52.67         100.21           0.4       b_rw
## 4 100.34 51.22 84.35 50.50          99.62           0.2       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 168.69 21.23 157.22 20.19         232.81           0.4 q_constant
## 2 103.96 12.75  82.61 10.41         281.03           1.0 b_constant
## 3 160.54 20.00 143.88 18.36         414.44           0.8       b_rw
## 4 117.53 14.47  96.57 12.23         349.63           1.0       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 275.74 19.99 273.53 19.88         259.43             0 q_constant
## 2  33.46  2.39  25.57  1.83         320.03             1 b_constant
## 3  91.33  6.57  81.51  5.89         543.24             1       b_rw
## 4  35.47  2.54  26.69  1.92         445.67             1       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 229.46 20.47 226.20 20.29         216.22             0 q_constant
## 2  40.44  3.53  31.29  2.77         266.42             1 b_constant
## 3  67.89  6.14  62.03  5.62         388.63             1       b_rw
## 4  39.48  3.46  32.36  2.89         323.02             1       b_ou
##     RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 108.14 21.27 98.97 19.84         139.02           0.4 q_constant
## 2  45.03  9.85 35.58  7.52         171.81           0.8 b_constant
## 3  45.66  9.89 40.13  8.48         197.00           1.0       b_rw
## 4  44.95  9.85 34.97  7.40         177.41           0.8       b_ou
plots_q_constant_NFR
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]